<template>
   <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" >
         <el-form-item label="模板名称" prop="templateName">
            <el-input
               v-model="queryParams.templateName"
               placeholder="请输入模板名称"
               clearable
               style="width: 200px"
            />
         </el-form-item>
         <el-form-item>
            <el-button color="#265DA4" :dark="isDark" icon="Search" @click="handleQuery" v-hasPermi="['manager:toolTemplate:query']">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
         </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
         <el-col :span="1.5">
            <el-button
               color="#265DA4"
               plain
               icon="Plus"
               @click="handleAdd"
               v-hasPermi="['manager:toolTemplate:add']"
            >新增</el-button>
         </el-col>
      </el-row>

      <el-table v-loading="loading" :data="classList" @selection-change="handleSelectionChange">
         <el-table-column label="序号" align="left" type="index" width="100" />
         <el-table-column label="模板名称" align="left" prop="templateName"  />
         <el-table-column label="模板内容" align="left" prop="templateContent"  />
         <el-table-column label="使用工具" align="left" prop="toolName"  />
         <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
            <template #default="scope">
               <el-button link style="color:#265DA4;" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manager:toolTemplate:edit']">修改</el-button>
               <el-button link style="color:#265DA4;" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manager:toolTemplate:remove']" >删除</el-button>
            </template>
         </el-table-column>
      </el-table>

      <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize"
         @pagination="getList"
      />

      <!-- 添加或修改对话框 -->
      <el-dialog :title="title" v-model="open" width="780px" append-to-body 
      class="dialogClass" close-icon="CloseBold"
    >
      <template #title>
         
        <span>{{title}}</span>
      </template>
         <el-form ref="noticeRef" :model="form" :rules="rules" label-width="100px">
            <el-row>
               <el-col :span="10">
                  <el-form-item label="模板名称:" prop="templateName">
                     <el-input v-model="form.templateName" placeholder="请输入模板名称"  />
                  </el-form-item>
                  <el-form-item label="使用工具:" prop="toolName">
                        <div class="time-box"> 
                          <el-input v-model="form.toolName" placeholder="请选择使用工具" class="inputDeep" />
                          <el-button color="#265DA4" @click="selectTool"
                            >选择</el-button
                          >
                        </div>
                  </el-form-item>
                  <el-form-item label="模板内容:" prop="templateContent" style="width:600px;">
                    <el-input type="textarea" rows="6" v-model.trim="form.templateContent"  placeholder="请输入模板内容"  />
                  </el-form-item>
                  </el-col>
            </el-row>
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button color="#265DA4"  @click="submitForm">确 定</el-button>
               <el-button @click="cancel" color="#265DA4" plain>取 消</el-button>
            </div>
         </template>
      </el-dialog>
       <!-- 工具选择 -->
    <el-dialog title="工具地址" v-model="openTool" width="780px" append-to-body >
      <div v-for="(item,index) in toolList" :key="index">
        <h3>{{ item.toolName }}</h3>
        <el-table :data="item.toolData">
         <el-table-column label="序号" align="left" type="index" width="100" />
         <el-table-column label="工具ID" align="left" prop="toolId"  />
         <el-table-column label="工具名称" align="left" prop="toolName"  />
         <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
            <template #default="scope">
               <el-button color="#265DA4" @click="handleSelect(scope.row)" >选择</el-button>
            </template>
         </el-table-column>
      </el-table>
      </div>
      
      <template #footer>
        <div class="dialog-footer">
          <el-button color="#265DA4" @click="submitTool">确 定</el-button>
          <el-button @click="openTool = false" color="#265DA4" plain>取 消</el-button>
        </div>
      </template>
    </el-dialog>
   </div>
</template>

<script setup name="Place">
import { getToolTemplate,toolTemplateAdd,toolTemplateEdit,toolTemplateDelete} from "@/api/manage/tempManage";
import {
  gettoolManager,
  
} from "@/api/manage/manage";
const { proxy } = getCurrentInstance();

const toolList = ref([]);
const classList = ref([]);
const rowValue = ref({});
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const openTool = ref(false);
const total = ref(0);
const title = ref("");
const knowList = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageSize: 10,
    pageNum:1,
    templateName: '',
  },
  rules: {
    templateName: [{ required: true, message: "模板名称不能为空", trigger: "blur" }],
    toolId: [{ required: true, message: "使用工具不能为空", trigger: "blur" }],
    templateContent: [{ required: true, message: "模板内容不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

function gettoolList() {
  loading.value = true;
  gettoolManager({
    pageSize: 1000,
    pageNum:1,
  }).then(response => {
    console.log(response,'response');
    toolList.value = response;
    
  });
}
/** 查询列表 */
function getList() {
  loading.value = true;
  getToolTemplate(queryParams.value).then(response => {
    classList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
function submitTool(){
  openTool.value = false
  form.value.toolId = rowValue.value.toolId
  form.value.toolName = rowValue.value.toolName
}
function handleSelect(row){
  rowValue.value = row
  
}
function selectTool(){
  openTool.value = true
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {};
  proxy.resetForm("noticeRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.noticeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增";
}
/**修改按钮操作 */
function handleUpdate(row) {
   
  reset();
   console.log(row,'f');
    open.value = true;
    title.value = "编辑";
    form.value.templateName = row.templateName
    form.value.templateContent = row.templateContent
    form.value.toolId = row.toolId
    form.value.toolName = row.toolName
    form.value.tmplateId = row.tmplateId
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate(valid => {
    if (valid) {
      if (form.value.tmplateId != undefined) {
        toolTemplateEdit({
          ...form.value,
          templateContent: form.value.templateContent.replace(/\s*/g,'')
        }).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        toolTemplateAdd({
          ...form.value,
          templateContent: form.value.templateContent.replace(/\s*/g,'')
        }).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const dictCode = row.dictCode
  proxy.$modal.confirm('是否确认删除"' + row.templateName + '"的数据项？').then(function() {
    return toolTemplateDelete(row.tmplateId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

gettoolList()
getList();
</script>
<style lang="scss" scoped>
:deep(.el-pagination.is-background .el-pager li:not(.is-disabled).is-active) {
  background-color: #265DA4 !important; //修改默认的背景色
}
.inputDeep {
      :deep(.el-input__inner) {
            width: 182px !important;
      }
}
.time-box {
  display: flex;
  justify-content: space-between;
  .el-date-editor {
    width: 206px;
  }
  .el-button {
    margin-left: 15px;
  }
}
</style>
